const app = getApp();

import {
  userEdit,
  getUserInfo,
  postWechatGetPhone
} from '../../api/user.js';
import {
  getIndexData
} from '../../api/api.js';
import authLogin from '../../utils/autuLogin.js';
import util from '../../utils/util.js';
import wxh from '../../utils/wxh.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    parameter: {
      'navbar': '1',
      'return': '0',
      'title': '个人中心',
    },
    userInfo: '',
    isGoIndex: false,
    iShidden: true,
    isAuto: false,
    isshow: false,
    service_hotline: ''
  },

  /**
   * 授权回调
   */
  onLoadFun: function (e) {
    this.getUserInfo();
  },
  /**
   * 获取个人用户信息
   */
  getUserInfo: function () {
    var that = this;
    getUserInfo().then(res => {
      that.setData({
        userInfo: res.data
      });
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  //打开修改名称
  openxg() {
    this.setData({
      isshow: !this.data.isshow
    })
  },

  //更新头像
  sc_tx(e) {
    console.log(e)
    let url = e.detail.avatarUrl;
    let that = this;
    util.Upload_img({
      url: url
    }, function (res) {
      let value = {}
      value.avatar = res.data.url;
      value.nickname = that.data.userInfo.nickname;
      userEdit(value).then(res => {
        that.getUserInfo();
      }).catch(msg => {
        return app.Tips({
          title: msg || '修改失败'
        })
      });
    })
  },
  getname(e) {
    this.setData({
      ['userInfo.nickname']: e.detail.value
    })
  },
  //修改名称   
  xgname(e) {
    console.log(e)
    let value = {}
    value.avatar = this.data.userInfo.avatar;
    value.nickname = e.detail.value.nickname;
    userEdit(value).then(res => {
      this.getUserInfo();
      this.setData({
        isshow: false
      })
    }).catch(msg => {
      return app.Tips({
        title: msg || '修改失败'
      })
    });
  },

  getPhoneNumber(e) {
    console.log(e)
    let data = {
      code: e.detail.code
    }
    wx.showLoading({
      title: ' ',
    })
    postWechatGetPhone(data).then(res => {
      wx.hideLoading()
      this.getUserInfo();
    }).catch(e => {
      wx.hideLoading()
    })
  },

  //页面跳转
  goPages(e) {
    let url = e.currentTarget.dataset.url
    if (!getApp().globalData.isLog) {
      this.setData({
        iShidden: false
      })
      return;
    }
    wx.navigateTo({
      url: url,
    })
  },
  //联系客服
  ddh() {
    let that = this;
    wx.makePhoneCall({
      phoneNumber: that.data.service_hotline
    })
  },
  // 跳转二维码
  goscan(e) {
    let url = e.currentTarget.dataset.url
    if (!getApp().globalData.isLog) {
      this.setData({
        iShidden: false
      })
      return;
    }
    wx.navigateTo({
      url: url,
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  onShow: function () {
    let that = this;
    if (app.globalData.isLog) {
      this.getUserInfo();
    }
    getIndexData().then(res => {
      that.setData({
        service_hotline: res.data.service_hotline
      })
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  onShareAppMessage: function (res) {
    let that = this;
    return {
      title: '大象搬家',
      path: '/pages/index/index?spread=' + that.data.userInfo.share_code,
      imageUrl: 'http://dxbj.jxnswl.cn//uploads/attach/2022/11/20221117/d47a1aba52db8209ea24ac26afcd219a.jpg',
      success: function (res) {
        console.log(res);
      }
    }
  }


})